package com.wzcl.app.model.vo;


import com.wzcl.app.dao.model.Course;
import com.wzcl.app.dao.model.Orders;
import com.wzcl.app.dao.model.UserCourse;
import com.wzcl.app.model.common.WZCL;
import com.wzcl.app.utils.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = false)
@Data
public class OrderVo extends BaseVo{
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "订单编号")
    private String no;

    @ApiModelProperty(value = "用户Id")
    private Integer userId;

    @ApiModelProperty(value = "用户手机")
    private String userPhone;

    @ApiModelProperty(value = "支付时间")
    private Date payTime;

    @ApiModelProperty(value = "状态")
    private Integer state;
    private String stateStr;

    @ApiModelProperty(value = "支付状态")
    private Integer payState;
    private String payStateStr;

    @ApiModelProperty(value = "订单价格(原价)")
    private String money;

    @ApiModelProperty(value = "商品价格（现价）")
    private String productMoney;

    @ApiModelProperty(value = "实际付款金额")
    private String payMoney;

    @ApiModelProperty(value = "用户姓名")
    private String userName;

    @ApiModelProperty(value = "系统类型")
    private Integer sysType;

    @ApiModelProperty(value = "配送地址")
    private SendPathVo sendPath;

    @ApiModelProperty(value = "订单状态")
    private Integer ordersState;
    private String ordersStateStr;

    @ApiModelProperty(value = "快递状态")
    private Integer deliveryState;
    private String deliveryStateStr;

    @ApiModelProperty(value = "是否需要快递货物")
    private Boolean needDelivery;
    private String needDeliveryStr;

    @ApiModelProperty(value = "生成订单的接口标记(不展示)")
    private String createFrom;

    @ApiModelProperty("课程信息")
    private Course course;

    @ApiModelProperty(value = "详情")
    private List<OrderDetailVo> orderDetailList;

    private OrderVo(){}

    public static OrderVo create(Orders orders){
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(orders,orderVo,"money","productMoney","payMoney","sendPath");
        orderVo.money = orders.getMoney().setScale(2, BigDecimal.ROUND_DOWN).toString();
        orderVo.productMoney = orders.getProductMoney().setScale(2, BigDecimal.ROUND_DOWN).toString();
        orderVo.payMoney = orders.getPayMoney().setScale(2, BigDecimal.ROUND_DOWN).toString();
        if(orders.getSendPath() != null){
            orderVo.sendPath = SendPathVo.create(orders.getSendPath());
        }
        if(orders.getDetails()!=null && orders.getDetails().size()>0){
            orderVo.orderDetailList =  orders.getDetails().stream().map(OrderDetailVo::create).collect(Collectors.toList());
        }
        orderVo.translationValueStr();
        return orderVo;
    }


    @Override
    public void translationValueStr() {
        Map<String, Map<Integer, String>> map = getTranslationMap();
        this.stateStr = map.get(WZCL.GROUP_NAME.STATE).get(this.state);
        this.payStateStr = map.get(WZCL.GROUP_NAME.PAY_STATE).get(this.payState);
        this.ordersStateStr = map.get(WZCL.GROUP_NAME.ORDER_STATE).get(this.ordersState);
        this.deliveryStateStr = map.get(WZCL.GROUP_NAME.DELIVERY_STATE).get(this.deliveryState);
        // TODO this.needDeliveryStr = this.needDelivery?"是":"否";
    }

    @Override
    public void processResources() {

    }
}
